package gui.popup;

import gui.App;
import gui.dialogs.Confirm;

import java.text.ParseException;
import java.util.Date;

import models.Workshop;
import models.Workshop.DagDeel;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Text;

public class WorkshopPopup extends Popup<Workshop>{
	
	private Text title, price, explaination, participantsMax, docent;
	private Combo dagDeel;
	private DateTime date;
	private Workshop workshop;
	
	public WorkshopPopup(App app) {
		super(app);
	}
	
	/**
	 * This function make's a copy of a excisting workshop. The selected workshop is copied and then filled in a replacement popup.
	 * @param workshopToCopy a workshop that needs to be copied.
	 * @return workshopCopy the new workshops with the adaptions of the replacementform.
	 */
	public Workshop open_Copy(Workshop workshopToCopy) {
		Workshop workshop = workshopToCopy.copy();
		workshop.setTitle(workshop.getTitle() + " (kopie)");
		return open_Replace(workshop);
	}

	/**
	 * this function returns empty workshop for adding a new workshop.
	 */
	@Override
	public Workshop createEmptyModel() {
		return new Workshop();
	}

	/**
	 * This function builds the popup. It calls all the functions to display the pieces of the popup.
	 */
	@Override
	public void buildForm(Composite formPanel, Workshop workshop) {
		this.workshop = workshop;
		setTitle_modelType("Workshop");
		
		formPanel.setLayout(new GridLayout(2, false));
		
		createFormItem_label(formPanel, "Titel");
		title = createFormItem_text(formPanel, workshop.getTitle());

		createFormItem_label(formPanel, "Datum");
		date = createFormItem_date(formPanel, workshop.getDate());

		createFormItem_label(formPanel, "Dagdeel");
		dagDeel = createFormItem_dagdeel(formPanel, workshop.getDagDeel());

		createFormItem_label(formPanel, "Prijs");
		price = createFormItem_text(formPanel, workshop.getPrice() + "");

		createFormItem_label(formPanel, "Uitleg");
		explaination = createFormItem_longtext(formPanel, workshop.getExplaination());

		createFormItem_label(formPanel, "Max. deelnemers");
		participantsMax = createFormItem_text(formPanel, workshop.getParticipantsMax() + "");

		createFormItem_label(formPanel, "Docent");
		docent = createFormItem_text(formPanel, workshop.getDocent());	
		
	}

	/**
	 * a checkfunction to confirm that the inserted information in the popup is valid
	 * 
	 * @return boolean when <code>true</code> Popup data is valid and update function is safe to use.
	 */
	@Override
	public boolean isDataValid() {
		ParseError errorBox = new ParseError();	
		
		String ti = title.getText();
		errorBox.obligatedField(ti);
		errorBox.fieldLength(ti, 50);
		
		String pr = price.getText();
		errorBox.obligatedField(pr);
		
		String ex = explaination.getText();
		errorBox.fieldLength(ex, 2000);
		
		String pa = participantsMax.getText();
		errorBox.obligatedField(pa);
		errorBox.fieldLength(pa, 50);
		
		String doc = docent.getText();
		errorBox.obligatedField(doc);
		errorBox.fieldLength(doc, 50);
		
		int max = -1;
		try {
			Double.parseDouble(pr);
			max = Integer.parseInt(pa);
		} catch (NumberFormatException e) {
			errorBox.parseError();
		}
		
		if(errorBox.errorsPresent()) {
			alertInvalidData(errorBox.getErrorString());
			return false;
		}
		
		int subscriptions = getApp().getSubscriptionList().getSubscriptions(workshop).size();
		if (subscriptions >= max) {
			boolean cont = new Confirm(getShell(), 
				"Maximumcapaciteit overschreden", 
				"Er zijn reeds meer inschrijvingen (" + subscriptions + ") dan het gestende maximum (" + max + ")! Toch doorgaan?"
			).open().accepted;
			if (!cont) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * function to update a workshop. This function update's the given workshop.
	 * 
	 * @param workshop a workshop to update with the data from the workshopPopup.
	 */
	@SuppressWarnings("deprecation")
	@Override
	public void updateModel(Workshop workshop) {
		workshop.setTitle(title.getText());
		workshop.setDate(new Date(date.getYear()-1900, date.getMonth(), date.getDay()));
		try {
			workshop.setDagDeel(DagDeel.parse(dagDeel.getText()));
		} catch (ParseException e) {
			throw new RuntimeException("Propgramming error: invalid dagDeel, but user can't affect it => propgrammers error.");
		}
		workshop.setPrice(Double.parseDouble(price.getText()));
		workshop.setExplaination(explaination.getText());
		workshop.setParticipantsMax(Integer.parseInt(participantsMax.getText()));
		workshop.setDocent(docent.getText());
	}
}
